package day04;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import work.JDBCUtils;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

public class demo {
    public static void main(String[] args) throws Exception {
        //加载properties文件的内容到Properties对象中
        Properties info = new Properties();
        //加载项目下的属性文件
        FileInputStream fis = new FileInputStream("druid.properties");
        //从输入流中加载属性
        info.load(fis);
        System.out.println(info);
        //创建DRUID连接池，使用配置文件中的参数
        DataSource dataSource = DruidDataSourceFactory.createDataSource(info);
        //从DRUID连接池中取出连接
        //Connection conn = dataSource.getConnection();
        //System.out.println("conn = " + conn);
// 需求: 根据用户名和密码 查询用户信息
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            // 获得连接
            conn = dataSource.getConnection();
            // 获得发送sql的对象
            String sql = "select * from exam where id=? ";
           ps = conn.prepareStatement(sql);
            // 如果有问号,需要 设置参数,注意:下标从1开始
            ps.setString(1, "1");
            // 执行sql 获得结果
            rs = ps.executeQuery();
            // 处理结果
            if (rs.next()) {
                int id = rs.getInt("id");
                String username = rs.getString("name");
                String pwd = rs.getString("chinese");

                System.out.println(id + "===" + username + "===" + pwd);
            } else {
                System.out.println("没有查到对应的用户信息!");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtils.release(conn, ps, rs);
        }
    }
    }


